package com.qdd.freight.util;

import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @ClassName HttpClientUtils
 * @Description TODO
 * @Author Yan
 * @Date 2019/12/27 15:32
 * @Version 1.0
 **/

public class HttpClientUtils {

    /**
     * 请求连接池
     */
    private static CloseableHttpClient httpClient;

    static {
        PoolingHttpClientConnectionManager connectionPool = new PoolingHttpClientConnectionManager();
        connectionPool.setMaxTotal(100);
        connectionPool.setDefaultMaxPerRoute(20);
        connectionPool.setDefaultMaxPerRoute(50);
        httpClient = HttpClients.custom().setConnectionManager(connectionPool).build();
    }


    /**
     * post请求，json数据
     * @param url
     * @param jsonString
     * @return
     *
     * companyCode=JINANDAOMIN&digest=ZGU0YTBmZGU5MTFjMzRhN2YxNmM5MTk4ZGZiYWUwMGQ=&params={"destCity":"济南市","destDistrict":"长清区","destProvince":"山东省","logisticCompanyID":"DEPPON","originalCity":"北京市","originalDistrict":"海淀区","originalProvince":"北京"}&timestamp=1577372047653
     */

    public static String post(String url, List<NameValuePair> params) {
        CloseableHttpResponse response = null;
        BufferedReader in = null;
        String result = "";
        try {
            HttpPost httpPost = new HttpPost(url);
            //创建自定义配置并设置请求
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
            httpPost.setConfig(requestConfig);
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
//            httpPost.setEntity(new StringEntity(data, Charset.forName("UTF-8")));//档data为自己拼接好的字符串用时使用
            httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));//调用父类方法自动拼接；&符号
            //执行请求
            response = httpClient.execute(httpPost);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            result = sb.toString();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭相关资源
            try {
                if (null != response) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
